import os
import boto3

def is_running_on_aws_lambda():
    """检测是否在真正的AWS Lambda环境中运行"""
    # 方法1：检查Lambda特有的环境变量
    if 'AWS_LAMBDA_FUNCTION_NAME' in os.environ:
        return True
    
    # 方法2：检查Lambda执行路径环境变量
    if 'LAMBDA_TASK_ROOT' in os.environ:
        return True
    
    return False

def get_boto3_client(service_name):
    if not is_running_on_aws_lambda():
        access_key = os.environ.get('AWS_ACCESS_KEY_ID_LAMBDA')
        secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY_LAMBDA')
        region = os.environ.get('AWS_REGION_LAMBDA')
        boto3_client = boto3.client(
            service_name=service_name,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
            region_name=region
        )
    else:
        boto3_client = boto3.client(service_name)
    return boto3_client

def get_boto3_resource(service_name):
    if not is_running_on_aws_lambda():
        access_key = os.environ.get('AWS_ACCESS_KEY_ID_LAMBDA')
        secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY_LAMBDA')
        region = os.environ.get('AWS_REGION_LAMBDA')
        boto3_resource = boto3.resource(
            service_name,
            aws_access_key_id=access_key,
            aws_secret_access_key=secret_key,
            region_name=region
        )
    else:
        boto3_resource = boto3.resource(service_name)
    return boto3_resource
